package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.Schedule;
import org.example.mapper.ScheduleMapper;
import org.example.service.ScheduleService;
import org.example.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Transactional(readOnly = true)
    @Override
    public PageBean<Schedule> queryAllByPage(Integer pageSize, Integer currentPage) {
        // 设置分页参数
        PageHelper.startPage(currentPage, pageSize);
        // 数据库查询
        List<Schedule> list = this.scheduleMapper.selectAllByPage();
        // 获取结果
        PageInfo<Schedule> pageInfo = new PageInfo<>(list);
        // pageBean 封装
        PageBean<Schedule> pageBean = new PageBean<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());

        log.info("分页查询结果:{}", pageBean);
        return pageBean;
    }

    @Override
    public void saveSchedule(Schedule schedule) {
        this.scheduleMapper.insert(schedule);
    }

    @Override
    public void removeById(Integer id) {
        this.scheduleMapper.delete(id);
    }

    @Override
    public void updateSchedule(Schedule schedule) {
        this.scheduleMapper.update(schedule);
    }

}
